#include "Gwen/Controls/Properties.h"
#include "Gwen/Controls/Property/Text.h"
#include "StomperLib/Controls/ObjectProperties.h"
#include "StomperLib/Controls/Property/PropertyFactory.h"
#include "StomperLib/Controls/Property/StringProperty.h"
#include "StomperLib/Controls/Property/Vector2Property.h"
namespace StomperLib {
namespace Controls {
GWEN_CONTROL_CONSTRUCTOR( ObjectProperties )
{
  this->Dock( Gwen::Pos::Fill );
  this->m_EditorObject = NULL;
  this->UpdateProperties( );
}
void ObjectProperties::SetEditorObject( StomperLib::EditorObjectInfo* object )
{
  this->m_EditorObject = object;
  this->UpdateProperties( );
}

void ObjectProperties::UpdateProperties( )
{
  this->Clear( );

  if (this->m_EditorObject == NULL)
  {
    return;
  }

  Gwen::Controls::Properties* baseProperties = this->Add( "EditorObject" );
  Gwen::Controls::Properties* objProperties = this->Add( "Properties" );
  objProperties->SetName( "Properties" );
  StomperLib::Controls::Property::PropetyFactory::CreateProperty( this->m_EditorObject, "editor name", "Name",
                                                                  baseProperties );
  for (auto propIter = this->m_EditorObject->PropertyInfos.begin( );
       propIter != this->m_EditorObject->PropertyInfos.end( ); ++propIter)
  {
    StomperLib::Controls::Property::PropetyFactory::CreateProperty( this->m_EditorObject, propIter->Field,
                                                                    propIter->DisplayName,
                                                                    objProperties );
  }
  this->ExpandAll( );
}

void ObjectProperties::RefreshProperties( )
{
  if (this->m_EditorObject == NULL)
  {
    return;
  }

  auto objProperties = this->FindChild<Gwen::Controls::Properties>( "Properties", true );
  if (objProperties == NULL)
    return;

  for (auto propIter = this->m_EditorObject->PropertyInfos.begin( );
       propIter != this->m_EditorObject->PropertyInfos.end( ); ++propIter)
  {
    auto prop = objProperties->FindChild<StomperLib::Controls::Property::TextPropertyBase>( propIter->DisplayName, true );

    if (prop == NULL)
      continue;

    prop->CopyDataToValue( );
  }
}

}
}